#! /usr/bin/python3
# -*- coding: UTF-8 -*-

if __name__ == "__main__":
	raise Exception("不支持从这里调用")

__version__ = "1.0"
__all__ = ["MouseChshHttpRequest"]

from com.mousechsh.common.middle.http.MouseChshHttpConst import MOUSECHSH_HTTP_METHOD_GET, MOUSECHSH_HTTP_VERSION_1_1
from com.mousechsh.common.middle.http.MouseChshHttpHeader import MouseChshHttpHeader
from com.mousechsh.common.middle.http.MouseChshHttpMultiPartBody import MouseChshHttpMultiPartBody
from com.mousechsh.common.util.MouseChshCodeUtil import mousechsh_codec, mousechsh_code_util_type_bytes, \
	mousechsh_code_util_type_utf8string


class MouseChshHttpRequest:

	def __init__(self):
		self.__method = MOUSECHSH_HTTP_METHOD_GET
		self.__path = '/'
		self.__version = MOUSECHSH_HTTP_VERSION_1_1
		self.__header = MouseChshHttpHeader()
		self.__body = ''
		self.__body_type = 'text'

	def get_method(self):
		return str(self.__method)

	def set_method(self, value):
		if isinstance(value, bytes):
			value = mousechsh_codec(
				value,
				source_type=mousechsh_code_util_type_bytes,
				target_type=mousechsh_code_util_type_utf8string
			)
		self.__method = str(value).upper()

	def get_path(self):
		return str(self.__path)

	def set_path(self, value):
		if isinstance(value, bytes):
			value = mousechsh_codec(
				value,
				source_type=mousechsh_code_util_type_bytes,
				target_type=mousechsh_code_util_type_utf8string
			)
		self.__path = str(value)

	def get_version(self):
		return str(self.__version)

	def set_version(self, value):
		if isinstance(value, bytes):
			value = mousechsh_codec(
				value,
				source_type=mousechsh_code_util_type_bytes,
				target_type=mousechsh_code_util_type_utf8string
			)
		self.__version = str(value).upper()

	def get_header(self):
		return self.__header

	def get_body(self):
		if self.__body_type == 'text':
			return str(self.__body)
		elif self.__body_type == 'binary':
			return self.__body
		elif self.__body_type == 'multipart':
			return self.__body
		else:
			return None

	def get_body_type(self):
		return self.__body_type

	def set_body(self, value):
		self.__body_type = 'text'

		value_str = mousechsh_codec(
			value,
			source_type=mousechsh_code_util_type_bytes,
			target_type=mousechsh_code_util_type_utf8string
		)
		if value_str:
			value = value_str

		if isinstance(value, bytes):
			if value[-2:] == b'\r\n':
				value = value[:-2]
			self.__body = value
			self.__body_type = 'binary'
		if self.__body_type == 'text':
			self.__body = str(value).strip()
		else:
			self.__body = value

	def to_string(self):
		result = ''
		result += self.get_method()
		result += ' '
		result += self.get_path()
		result += ' '
		result += self.get_version()
		result += '\r\n'
		result += self.get_header().to_string()
		result += '\r\n'
		if self.get_body_type() == 'text':
			result += self.get_body()
		elif self.get_body_type() == 'binary':
			result += '<<binary length=' + str(len(self.get_body())) + '>>'
		elif self.get_body_type() == 'multipart':
			result += self.__body.to_string()
		if len(self.__body) > 0:
			result += '\r\n'
		result += '\r\n'
		return result

	def to_hex(self):
		result = b''
		result += mousechsh_codec(
			self.get_method(),
			source_type=mousechsh_code_util_type_utf8string,
			target_type=mousechsh_code_util_type_bytes
		)
		result += b' '
		result += mousechsh_codec(
			self.get_path(),
			source_type=mousechsh_code_util_type_utf8string,
			target_type=mousechsh_code_util_type_bytes
		)
		result += b' '
		result += mousechsh_codec(
			self.get_version(),
			source_type=mousechsh_code_util_type_utf8string,
			target_type=mousechsh_code_util_type_bytes
		)
		result += b'\r\n'
		result += self.get_header().to_hex()
		result += b'\r\n'
		if self.get_body_type() == 'text':
			result += mousechsh_codec(
				self.get_body(),
				source_type=mousechsh_code_util_type_utf8string,
				target_type=mousechsh_code_util_type_bytes
			)
		elif self.get_body_type() == 'binary':
			result += self.get_body()
		elif self.get_body_type() == 'multipart':
			result += self.__body.to_hex()
		if len(self.__body) > 0:
			result += b'\r\n'
		result += b'\r\n'
		return result

	def parse(self, text):
		if isinstance( text, bytes ):
			idx = text.find(b' ')
			if idx < 0:
				return
			self.set_method(text[0: idx])
			text = text[idx + 1:]
			idx = text.find(b' ')
			if idx < 0:
				return
			self.set_path(text[0: idx])
			text = text[idx + 1:]
			idx = text.find(b'\r\n')
			if idx < 0:
				return
			self.set_version(text[0: idx])
			text = text[idx + 2:]

			idx = text.find(b'\r\n\r\n')
			if idx < 0:
				return
			self.get_header().parse(text[0: idx])

			self.set_body(text[idx + 4:])

			self.make_multipart()

	def make_multipart(self):
		if self.get_body_type() == 'binary' and self.get_header().get_content_type() == 'form':
			multi = MouseChshHttpMultiPartBody(self.get_header().get_multipart_form_data_boundary())
			multi.parse(self.get_body())
			self.__body = multi
			self.__body_type = 'multipart'
